﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using SkynetCE.Foundation.Services.Navigation;
using SkynetCE.Foundation.Services.DatabaseService;
using System.Data.SqlServerCe;
using System.Windows.Forms;
using Infrastructure.CompactCE.ExceptionHandling;

namespace SkynetCE.Administration.Views.MgmCategories
{
    public class MgmCategoriesPresenter : Foundation.Views.Master.MasterPresenter<IMgmCategoriesView>
    {
        public Foundation.Model.SkynetModel Model { protected get; set; }
        public Foundation.Model.SkynetModel.ExamCategoriesRow CurrentCategory { get; set; }

        public IDatabaseService DatabaseService { protected get; set; }
        public Foundation.Model.SkynetModelTableAdapters.ExamCategoriesTableAdapter Adapter { protected get; set; }
        public MgmCategoriesPresenter()
        {
            this.Model = new SkynetCE.Foundation.Model.SkynetModel();
            this.DatabaseService = NavigationService.ResolveService<IDatabaseService>(); 
        }        
        public override void OnViewReady()
        {

            this.Adapter = new Foundation.Model.SkynetModelTableAdapters.ExamCategoriesTableAdapter();
            this.Adapter.Connection = new SqlCeConnection(this.DatabaseService.ApplicationConnectionString);
            this.Adapter.ClearBeforeFill = true;
            this.Adapter.Fill(this.Model.ExamCategories); 
            this.View.Model = this.Model; 
        }

        internal void AddNewCategory()
        {
            try
            {
                this.CurrentCategory = this.Model.ExamCategories.NewRow() as Foundation.Model.SkynetModel.ExamCategoriesRow;
                this.CurrentCategory.Id = Guid.NewGuid();
                this.CurrentCategory.Title = "enter a title";
                this.CurrentCategory.Description = "enter description";
                this.Model.ExamCategories.Rows.Add(this.CurrentCategory);

                this.View.Title = this.CurrentCategory.Title;
                this.View.Description = this.CurrentCategory.Description; 
            }
            catch (Exception ex)
            {
                bool rethrow =  ExceptionPolicy.HandleException(ex, Infrastructure.CompactCE.ExceptionHandling.ExceptionPolicyNames.SmartViewPolicy);
                if (rethrow)
                    throw; 
            }            
        }

        internal void SaveCategories()
        {

            if (this.CurrentCategory != null)
            {
                this.CurrentCategory.Title = this.View.Title;
                this.CurrentCategory.Description = this.View.Description;
                if (this.CurrentCategory != null && !this.Model.HasErrors)
                {
                    this.Adapter.Update(this.Model);        
                }
            }                        
        }
        internal string ValidateExamCategory(Foundation.Model.SkynetModel.ExamCategoriesRow item)
        {

            StringBuilder sb = new StringBuilder();  
            if (string.IsNullOrEmpty(item.Title) && item.Title == "")
                sb.AppendLine("You must be enter a value");
            return sb.ToString();
        }

        internal void SetCurrentCategory(int index)
        {
            if (index < 0)
                index = 0;           
            
            this.CurrentCategory = this.Model.ExamCategories.Rows[index] as Foundation.Model.SkynetModel.ExamCategoriesRow;
            this.View.Title = this.CurrentCategory.Title;
            this.View.Description = this.CurrentCategory.Description; 
        }

        internal void DeleteCategory(int index)
        {
            if (index < 0)
                index = 0;

            this.CurrentCategory = this.Model.ExamCategories.Rows[index] as Foundation.Model.SkynetModel.ExamCategoriesRow;
            this.CurrentCategory.Delete(); 
        }
    }
}
